﻿using Core;
using RimWorld;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Verse;
using Verse.Noise;
using Verse.Sound;
using WhoXiuXian;
using static RimWorld.PsychicRitualRoleDef;
using static System.Collections.Specialized.BitVector32;

namespace RI_Talisman
{
    public class CompAbilityEffect_RI_Repair : CompAbilityEffect
    {
        public new CompProperties_RI_Repair Props => (CompProperties_RI_Repair)props;

        private static Vector3 MoteCastOffset = new Vector3(0f, 0f, 0);
        public override void Initialize(AbilityCompProperties props)
        {
            base.Initialize(props);
        }

        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest)
        {
            List<Thing> things = new List<Thing>();
            List<IntVec3> cells = AffectedCells(target, parent.pawn.Map, Props.radius).ToList<IntVec3>();
            for (int x = 0; x < cells.Count; x++)
            {
                if (cells[x].GetFirstPawn(parent.pawn.Map) != null || cells[x].GetFirstItem(parent.pawn.Map) != null || cells[x].GetFirstBuilding(parent.pawn.Map) != null)
                {
                    foreach (Thing thing in cells[x].GetThingList(parent.pawn.Map))
                    {
                        if (thing?.HitPoints != -1)
                        {
                            things.Add(thing);
                        }
                    }
                    var pw = cells[x].GetFirstPawn(parent.pawn.Map);
                    if (pw != null)
                    { 
                        things.Add(pw);
                    }
                    
                    ThingDef moteCast = Props.moteCastDef;
                    if (cells[x].GetFirstItem(parent.pawn.Map) != null)
                    {
                        MoteMaker.MakeAttachedOverlay(cells[x].GetFirstItem(parent.pawn.Map), moteCast, MoteCastOffset, 1);
                    }
                }

            }
            foreach (Thing item in things)
            {
                if (item != null)
                {
                    if (item is Building building)
                    {
                        if (building.HitPoints + 200 < building.MaxHitPoints)
                        {
                            building.HitPoints += 200;
                        }
                        else
                        {
                            building.HitPoints = building.MaxHitPoints;
                        }
                    }
                    if (item is Pawn pawn)
                    {
                        #if DEBUG
                        Log.Message("pawn: "+pawn.Label);
                        #endif
                        foreach (Thing thing in pawn.GetAllItems())
                        {
                            #if DEBUG
                            Log.Message("item: "+thing.Label);
                            #endif
                            if (thing.HitPoints + 0.5 * thing.MaxHitPoints < thing.MaxHitPoints)
                            {
                                thing.HitPoints += Convert.ToInt32(0.5 * thing.MaxHitPoints);
                            }
                            else
                            {
                                thing.HitPoints = thing.MaxHitPoints;
                            }
                        }
                    }
                    if (item is Apparel apparel)
                    {
                        if (apparel.HitPoints + 0.5 * item.MaxHitPoints < apparel.MaxHitPoints)
                        {
                            apparel.HitPoints += Convert.ToInt32(0.5 * item.MaxHitPoints);
                        }
                        else
                        {
                            apparel.HitPoints = apparel.MaxHitPoints;
                        }
                    }
                    if (item.def.IsWeapon)
                    {
                        if (item.HitPoints + 0.5 * item.MaxHitPoints < item.MaxHitPoints)
                        {
                            item.HitPoints += Convert.ToInt32(0.5 * item.MaxHitPoints);
                        }
                        else
                        {
                            item.HitPoints = item.MaxHitPoints;
                        }
                    }
                }
            }
        }

        public override void CompTick()
        {

            base.CompTick();

        }
        private IEnumerable<IntVec3> AffectedCells(LocalTargetInfo target, Map map, int radius)
        {
            foreach (IntVec3 item in GenRadial.RadialCellsAround(target.Cell, radius, useCenter: true))
            {
                if (item.InBounds(map))
                {
                    yield return item;
                }
            }
        }
    }
}